<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
  <div style="max-height: 600px; overflow: auto;">
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form" layout="vertical">
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="工序">
              <j-dict-select-tag
                dictCode="working_procedure"
                v-model="model.workingProcedure"
                placeholder="请选择工序"
                disabled
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品种代号">
              <a-input placeholder="请输入品种代号" v-model="model.productCode" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品名" prop="productType">
              <a-input placeholder="请输入品名" v-model:value="model.productType" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品种全称">
              <a-input placeholder="请输入品种全称" v-model="model.productTypeFullName" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品种">
              <a-input placeholder="请输入品种" v-model="model.productCategory" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="纺纱方式">
              <a-input placeholder="请输入纺纱方式" v-model="model.spinningMethod" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="类别" prop="category">
              <a-input placeholder="请输入类别" v-model:value="model.category" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品种批号" prop="productBatch">
              <a-input placeholder="请输入品种批号" v-model="model.productBatch" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="品种支别">
              <a-input-number placeholder="请输入品种支别" v-model="model.productBranch" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="锭数" prop="ingots">
              <a-input-number placeholder="请输入锭数" v-model="model.ingots" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="定量换数系数-棉">
              <a-input-number
                placeholder="请输入定量换数系数-棉"
                v-model="model.commutationCotton"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="定量换数系数-其他">
              <a-input-number
                placeholder="请输入定量换数系数-其他"
                v-model="model.commutationOther"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="定量换数系数-综合">
              <a-input-number
                placeholder="请输入定量换数系数-综合"
                v-model="model.commutationComprehensive"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="定量" prop="ration">
              <a-input-number placeholder="请输入定量" v-model="model.ration" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="车速">
              <a-input-number placeholder="请输入车速" v-model="model.speed" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="效率">
              <a-input-number placeholder="请输入效率" v-model="model.efficiency" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="长度百米">
              <a-input-number placeholder="请输入长度百米" v-model="model.length" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="重量KG/日/台">
              <a-input-number placeholder="请输入重量" v-model="model.weight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="单锭重量KG/日/台">
              <a-input-number placeholder="请输入单锭重量" v-model="model.ingotWeight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="重量时间单位">
              <a-input-number placeholder="请输入重量时间单位" v-model="model.weightTimeUnit" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="定额看台（台）">
              <a-input placeholder="请输入定额看台" v-model="model.fixedMachinesNum" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="损耗率">
              <a-input placeholder="请输入损耗率" v-model="model.lossRate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="实际车速">
              <a-input placeholder="请输入实际车速" v-model="model.actualSpeed" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="管色">
              <a-input placeholder="请输入管色" v-model="model.tubeColor" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="内袋">
              <a-input placeholder="请输入内袋" v-model="model.innerPocket" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="捆绳">
              <a-input placeholder="请输入捆绳" v-model="model.twine" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="外袋">
              <a-input placeholder="请输入外袋" v-model="model.outPocket" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item style="width: 220px" label="成包标准毛重">
              <a-input placeholder="请输入成包标准毛重" v-model="model.standardGrossWeight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </div>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'
export default {
  name: 'XishaModal',
  components: {
    JEditor,
    JSwitch
  },
  data() {
    return {
      title: '操作',
      visible: false,
      disable: true,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 9 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      rules: {
        productType: [{ required: true, message: '请输入品种', trigger: 'blur' }],
        productBatch: [{ required: true, message: '请输入批号', trigger: 'blur' }],
        productBranch: [{ required: true, message: '请输入支号', trigger: 'blur' }],
        productCode: [{ required: true, message: '请输入代号', trigger: 'blur' }]
      },
      url: {
        add: '/crm_product_type_spun_yarn/crmProductTypeSpunYarn/add',
        edit: '/crm_product_type_spun_yarn/crmProductTypeSpunYarn/edit'
      }
    }
  },
  computed: {
    ration() {
      return (this.model.commutationComprehensive / this.model.productBranch / 10).toFixed(3)
    },
    length() {
      return ((this.model.speed * this.model.efficiency * this.model.weightTimeUnit * 60) / 100).toFixed(2)
    },
    weight() {
      return (
        (this.model.speed * 60 * this.model.weightTimeUnit * this.model.ration * this.model.ingots) /
        100000
      ).toFixed(2)
    },
    ingotWeight() {
      return ((this.model.speed * 60 * this.model.weightTimeUnit * this.model.ration) / 100000).toFixed(2)
    }
  },
  watch: {
    ration: {
      immediate: true,
      handler(val) {
        if (val) {
          this.model.ration = val
        }
      }
    },
    length: {
      immediate: true,
      handler(val) {
        if (val) {
          this.model.length = val
        }
      }
    },
    weight: {
      immediate: true,
      handler(val) {
        if (val) {
          this.model.weight = val
        }
      }
    },
    ingotWeight: {
      immediate: true,
      handler(val) {
        if (val) {
          this.model.ingotWeight = val
        }
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.disable = false
      this.model = Object.assign({}, { workingProcedure: 6 })
      this.model.workingProcedure = 6
      this.edit({}, 1)
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
    },

    handleOk() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped></style>
